package com.pig4cloud.pig.module.controller.product;

import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.product.ProductCombination;
import com.pig4cloud.pig.module.service.product.IProductCombinationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 拼团产品Controller
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@Validated
@Api(value = "拼团产品控制器", tags = {"拼团产品管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/product/combination")
public class ProductCombinationController {

    private final IProductCombinationService productCombinationService;

	@Value("${ruoyi.imgUrl}")
	private String imgUrl;

//	@Value("${ruoyi.imgUrlOss}")
//	private String imgUrlOss;

    /**
     * 查询拼团产品列表
     */
    @ApiOperation("查询拼团产品列表")
    @PreAuthorize("@pms.hasPermission('module:combination:list')")
    @GetMapping("/list")
    public TableDataInfo<ProductCombination> list(@Validated ProductCombination productCombination) {
        return PageUtils.buildDataInfo(productCombinationService.page(PageUtils.buildPage()));
    }

    /**
     * 获取拼团产品详细信息
     */
    @ApiOperation("获取拼团产品详细信息")
    @PreAuthorize("@pms.hasPermission('module:combination:query')")
    @GetMapping("/{id}")
    public R<ProductCombination> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return R.ok(productCombinationService.getById(id));
    }

    /**
     * 新增拼团产品
     */
    @ApiOperation("新增拼团产品")
    @PreAuthorize("@pms.hasPermission('module:combination:add')")
    @SysLog("拼团产品")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody ProductCombination productCombination) {
        return R.okByRow(productCombinationService.save(productCombination) ? 1 : 0);
    }

    /**
     * 修改拼团产品
     */
    @ApiOperation("修改拼团产品")
    @PreAuthorize("@pms.hasPermission('module:combination:edit')")
    @SysLog("拼团产品")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody ProductCombination productCombination) {
        return R.okByRow(productCombinationService.updateById(productCombination) ? 1 : 0);
    }

    /**
     * 删除拼团产品
     */
    @ApiOperation("删除拼团产品")
    @PreAuthorize("@pms.hasPermission('module:combination:remove')")
    @SysLog("拼团产品")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return R.okByRow(productCombinationService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
